package cn.xsly.pet.service.impl;

import cn.xsly.basic.service.IBaiduAiAuditService;
import cn.xsly.basic.util.*;
import cn.xsly.org.domain.Employee;
import cn.xsly.org.domain.Shop;
import cn.xsly.org.mapper.EmployeeMapper;
import cn.xsly.org.mapper.ShopMapper;
import cn.xsly.pet.domain.SearchMasterMsg;
import cn.xsly.pet.domain.SearchMasterMsgAuditLog;
import cn.xsly.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.xsly.pet.mapper.SearchMasterMsgMapper;
import cn.xsly.pet.query.SearchMasterMsgQuery;
import cn.xsly.pet.service.ISearchMasterMsgService;
import cn.xsly.basic.service.impl.BaseServiceImpl;
import cn.xsly.user.domain.Logininfo;
import cn.xsly.user.domain.User;
import cn.xsly.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private IBaiduAiAuditService iBaiduAiAuditService;
    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;


    @Override
    public AjaxResult publish(SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        //获取寻主信息
        Logininfo logininfo = LoginContext.getLogininfo(request);
        //登录信息找寻主的用户信息
        User user = userMapper.findByLoginId(logininfo.getId());
        //保存寻主信息
        searchMasterMsg.setUserId(user.getId());
        searchMasterMsgMapper.add(searchMasterMsg);
        //审核寻主信息
        Boolean boo1 = iBaiduAiAuditService.textAudit(searchMasterMsg.getTitle() + searchMasterMsg.getName());
        Boolean boo2 = iBaiduAiAuditService.imageAudit(searchMasterMsg.getResources());
        SearchMasterMsgAuditLog log = new SearchMasterMsgAuditLog();
        //Boolean boo2 =true;
        if (boo1 && boo2) {
            //审核通过寻主消息修改状态和审核日志保存状态和日志？？？？
            searchMasterMsg.setState(1);
            log.setState(1);
            log.setNote("发布成功");
            searchMasterMsgAuditLogMapper.add(log);
            //分配店铺
            //1.查店铺位置和寻主位置，工具得出最近的店铺位置
            List<Shop> shops = shopMapper.findAll();
            Point point = DistanceUtils.getPoint(searchMasterMsg.getAddress());
            Shop nearestShop = DistanceUtils.getNearestShop(point, shops);
            if (nearestShop == null) {
                searchMasterMsg.setState(3);
            } else {
                searchMasterMsg.setShopId(nearestShop.getId());
                Employee employee = employeeMapper.findById(nearestShop.getAdminId());
                System.out.println(employee.getPhone() + "你有新订单请去" + searchMasterMsg.getAddress());
            }
            searchMasterMsgMapper.update(searchMasterMsg);
            //审核失败,更改寻主消息状态，和保存审核日志
        } else {
            searchMasterMsg.setState(-1);
            searchMasterMsgMapper.update(searchMasterMsg);

            log.setState(0);
            log.setNote("发布失败");
            searchMasterMsgAuditLogMapper.add(log);
            System.out.println("发布失败");
            return AjaxResult.me().setSuccess(false).setMessage("寻主发布失败");
        }
        return new AjaxResult();
    }

    //设置员工店铺
    @Override
    public PageList<SearchMasterMsg> toHandle(SearchMasterMsgQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.findByLoginId(logininfo.getId());
        if (employee.getShopId() != null) {
            query.setShopId(employee.getShopId());
        }
        return super.queryPage(query);
    }

    @Override
    public PageList<SearchMasterMsg> user(SearchMasterMsgQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        User user = userMapper.findByLoginId(logininfo.getId());
        query.setUserId(user.getId());
        return super.queryPage(query);
    }

    @Override
    public PageList<SearchMasterMsg> pool(SearchMasterMsgQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.findByLoginId(logininfo.getId());
        query.setShopId(employee.getShopId());
        return super.queryPage(query);
    }

    @Override
    public PageList<SearchMasterMsg> finish(SearchMasterMsgQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.findByLoginId(logininfo.getId());
        query.setShopId(employee.getShopId());
        return super.queryPage(query);
    }

    @Override
    public void reject(Long id) {
        searchMasterMsgMapper.reject(id);
    }

}
